using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The TimeTableDetailDB class is responsible for interacting with the database to retrieve and store information
    /// about TimeTableDetail objects.
    /// </summary>
    public class TimeTableDetailDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of TimeTableDetail from the underlying datasource.
        /// </summary>
        /// <param name="timeTableKey">The unique TimeTableKey of the TimeTableDetail in the database.</param>
        /// <returns>An TimeTableDetail when the TimeTableKey was found in the database, or null otherwise.</returns>
        public static TimeTableDetail GetItem(int timeTableKey)
        {
            TimeTableDetail myTimeTableDetail = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTimeTableDetail = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTimeTableDetail;
            }
        }

        //To show Class Room Description
        public static DataTable GetItemByTeacherProfileKey(int timeTableKey, int teacherProfileKey, Int16 dayCode, Int16 sectionNo)
        {
            DataTable myTimeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailSelectSingleItemByTeacherProfileKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);
                    myCommand.Parameters.AddWithValue("@teacherProfileKey", teacherProfileKey);
                    myCommand.Parameters.AddWithValue("@dayCode", dayCode);
                    myCommand.Parameters.AddWithValue("@sectionNo", sectionNo);
                    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
                    myConnection.Open();

                    myTimeTable = new DataTable("tblTimeTableByTeacherProfileKey");
                    myAdapter.Fill(myTimeTable);

                    myConnection.Close();
                }
                return myTimeTable;
            }
        }

        /// <summary>
        /// Gets an instance of TimeTableDetail from the underlying datasource.
        /// </summary>
        /// <param name="timeTableKey">The unique TimeTableKey of the TimeTableDetail in the database.</param>
        /// <returns>An TimeTableDetail when the TimeTableKey was found in the database, or null otherwise.</returns>
        public static TimeTableDetail GetItemBySectionNo(int timeTableKey, int classRoomKey, Int16 dayCode, Int16 sectionNo)
        {
            TimeTableDetail myTimeTableDetail = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailSelectSingleItemBySectionNo", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", classRoomKey);
                    myCommand.Parameters.AddWithValue("@dayCode", dayCode);
                    myCommand.Parameters.AddWithValue("@sectionNo", sectionNo);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTimeTableDetail = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTimeTableDetail;
            }
        }

        /// <summary>
        /// Returns a list with TimeTableDetail objects.
        /// </summary>
        /// <returns>A generics List with the TimeTableDetail objects.</returns>
        public static TimeTableDetailList GetList()
        {
            TimeTableDetailList tempList = new TimeTableDetailList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with TimeTableDetail objects.
        /// </summary>
        /// <returns>A generics List with the TimeTableDetail objects.</returns>
        public static TimeTableDetailList GetListByDayCode(int TimeTableKey, int ClassRoomKey, Int16 DayCode)
        {
            TimeTableDetailList tempList = new TimeTableDetailList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailSelectListByDayCode", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", TimeTableKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
                    myCommand.Parameters.AddWithValue("@dayCode", DayCode);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        public static DataTable GetListTableByClassRoomKey(int TimeTableKey, int ClassRoomKey)
        {
            DataTable myTimeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailSelectListViewByClassRoomKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", TimeTableKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
                    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
                    myConnection.Open();

                    myTimeTable = new DataTable("tblTimeTableByClassRoom");
                    myAdapter.Fill(myTimeTable);

                    myConnection.Close();
                }
                return myTimeTable;
            }
        }
        /// <summary>
        /// Saves a TimeTableDetail in the database.
        /// </summary>
        /// <param name="myTimeTableDetail">The TimeTableDetail instance to save.</param>
        /// <returns>The new TimeTableKey if the TimeTableDetail is new in the database or the existing TimeTableKey when an item was updated.</returns>
        public static int Save(TimeTableDetail myTimeTableDetail)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myTimeTableDetail.TimeTableDetailKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@timeTableDetailKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@timeTableDetailKey", myTimeTableDetail.TimeTableDetailKey);
                    }

                    if (myTimeTableDetail.TimeTableKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@timeTableKey", 0);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@timeTableKey", myTimeTableDetail.TimeTableKey);
                    }

                    if (myTimeTableDetail.TeacherProfileKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@teacherProfileKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@teacherProfileKey", myTimeTableDetail.TeacherProfileKey);
                    }
                    if (myTimeTableDetail.SubjectKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", myTimeTableDetail.SubjectKey);
                    }
                    if (myTimeTableDetail.ClassRoomKey < 0)
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", myTimeTableDetail.ClassRoomKey);
                    }
                    if (myTimeTableDetail.DayCode < 0)
                    {
                        myCommand.Parameters.AddWithValue("@dayCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@dayCode", myTimeTableDetail.DayCode);
                    }
                    if (myTimeTableDetail.SectionNo < 0)
                    {
                        myCommand.Parameters.AddWithValue("@sectionNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@sectionNo", myTimeTableDetail.SectionNo);
                    }
                    if (myTimeTableDetail.StartTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@startTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@startTime", myTimeTableDetail.StartTime);
                    }
                    if (myTimeTableDetail.EndTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@endTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@endTime", myTimeTableDetail.EndTime);
                    }

                    myCommand.Parameters.AddWithValue("@status", myTimeTableDetail.Status);

                    if (myTimeTableDetail.UserID <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@userID", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userID", myTimeTableDetail.UserID);
                    }
                    
                    //if (myTimeTableDetail.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myTimeTableDetail.CreatedDateTime);
                    //}
                    //if (myTimeTableDetail.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myTimeTableDetail.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a TimeTableDetail from the database.
        /// </summary>
        /// <param name="timeTableKey">The TimeTableKey of the TimeTableDetail to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int timeTableKey, int timeTableDetailKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDetailDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@timeTableDetailKey", timeTableDetailKey);
                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the TimeTableDetail class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static TimeTableDetail FillDataRecord(IDataRecord myDataRecord)
        {
            TimeTableDetail myTimeTableDetail = new TimeTableDetail();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TimeTableDetailKey")))
            {
                myTimeTableDetail.TimeTableDetailKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TimeTableDetailKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TimeTableKey")))
            {
                myTimeTableDetail.TimeTableKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TimeTableKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherProfileKey")))
            {
                myTimeTableDetail.TeacherProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherProfileKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectKey")))
            {
                myTimeTableDetail.SubjectKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SubjectKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomKey")))
            {
                myTimeTableDetail.ClassRoomKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DayCode")))
            {
                myTimeTableDetail.DayCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DayCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SectionNo")))
            {
                myTimeTableDetail.SectionNo = myDataRecord.GetInt16(myDataRecord.GetOrdinal("SectionNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartTime")))
            {
                //string _startTime = myDataRecord.GetValue(myDataRecord.GetOrdinal("StartTime")).ToString();
                //int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
                //int _min = Convert.ToInt32(_startTime.Substring(2, 2));
                //myTimeTableDetail.StartTime = new TimeSpan(_hour, _min, 0);

                myTimeTableDetail.StartTime = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("StartTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndTime")))
            {
                //string _endTime = myDataRecord.GetValue(myDataRecord.GetOrdinal("EndTime")).ToString();
                //int _hour = Convert.ToInt32(_endTime.Substring(0, 2));
                //int _min = Convert.ToInt32(_endTime.Substring(2, 2));
                //myTimeTableDetail.EndTime = new TimeSpan(_hour, _min, 0);

                myTimeTableDetail.EndTime = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("EndTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myTimeTableDetail.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserID")))
            {
                myTimeTableDetail.UserID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserID"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myTimeTableDetail.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myTimeTableDetail.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myTimeTableDetail;
        }
    }

}
